﻿/**
 * 
 * Copyright 2009, 2010 Mike Jamer [email: mikejamer@gmail.com ]
 * 
 * This File is part of Moose IRC
 * 
 * MooseIRC is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * MooseIRC is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with MooseIRC.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */

using System;
using moose.extension;

namespace moose.irc.standard {

    /// <summary>
    /// Disconnect from the server,
    /// pass in quit message if desired.
    /// </summary>
    [CLSCompliant(true)]
    [Command(
        Name = "moose-cmd-disconnect",
        Description = "Request Core disconnect from IRC server",
        Author = "Mike MacKenzie-Jamer <email: mikejamer@gmail.com>",
        Version = "0.1")]
    [Trigger(TriggerText = "DISCONNECT")]
    public sealed class DisconnectCommand : IrcAction {

        protected override IrcState valid_states() {
            return IrcState.Connected | IrcState.Registered;
        }

        protected override void action(IrcClient client, IrcMessage message) {
            if (message.Arguments.Length > 0) {
                client.send_raw("QUIT :" + message.Arguments[0]);
            } else {
                client.send_raw("QUIT");
            }
            client.disconnect(); //The server has already dropped the connection, but we will call disconnect anyways, to clean the rest up

            IrcState old_state = client.SessionState;
            client.SessionState = IrcState.Disconnected;

            client.notify_event(new IrcEvent(IrcEvents.server_disconnected));

            client.echo("Disconnected...");
            return;
        }
    }
}
